Skip to content

perf: trim the answer payload to what the client renders (round 2, phase 4)#482

Merged
BigSimmo merged 1 commit into
claude/perf-r2-auth-roundtripfrom
claude/perf-r2-payload-trim
Jul 11, 2026
Merged

perf: trim the answer payload to what the client renders (round 2, phase 4)#482
BigSimmo merged 1 commit into
claude/perf-r2-auth-roundtripfrom
claude/perf-r2-payload-trim

Conversation

@BigSimmo

Copy link
Copy Markdown
Owner

Summary

Phase 4 of the round-2 performance initiative (stacked on #481). The final SSE event (and non-stream answer JSON) shipped every source's full chunk content plus server-only context — one large frame landing after the prose had already streamed, i.e. pure transfer/serialize tail on perceived answer completion.

toClientAnswerPayload trims sources at the route boundary only:

  • Dropped (audited: zero client consumers): adjacent_context, memory_cards, table_facts, index_unit, document_summary
  • Truncated: content → 700 chars at a word boundary; the client reads it only as the snippet fallback (retrieval_synopsis ?? content in answer-render-policy.ts:186)
  • Untouched: retrieval_synopsis, source_metadata, document_labels, all scoring/identity fields, visualEvidence, smartPanel, quote cards

Audit notes: follow-up questions never POST sources back to the server; answer-thread storage keeps sources client-side for the same render paths, so trimmed sources round-trip safely.

Ordering & purity contract (documented at both call sites): sourceGovernanceWarnings and logAnswerDiagnostics consume the FULL answer before the trim; the trim is non-mutating so both cache layers keep full sources; rag.ts and eval behavior are byte-identical.

Measured: a representative 8-source answer payload shrinks by >50% (unit-tested lower bound — real chunks are typically larger than the fixture).

Clinical governance preflight

  • npm run verify:cheap components: lint (0 warnings), typecheck, targeted vitest green (full vitest on the stack passed in perf: batch image signed URLs + cache seeded catalog fetches (round 2, phase 2) #479; only additive test files since)
  • npm run verify:ui124 passed / 0 failed (answer surface, source cards, citation jump, document viewer, drawers) with phases 0–4 stacked
  • npm run format:check — clean
  • eval:retrieval:qualitynot required: no retrieval, ranking, selection, chunking, or scoring behavior changed (trim happens after the answer is fully computed and cached)
  • eval:rag/eval:qualitynot required: answer generation, synthesis prompt, and post-processing untouched (server-side answer object is byte-identical; only the wire payload shrinks)
  • Source-backed claims: citation/verification flows unchanged — sources keep identity, synopsis, metadata, and governance fields
  • No patient-identifiable workflow introduced
  • Supabase target unchanged; service-role keys remain server-only
  • Demo/synthetic separation unchanged
  • Source metadata / review-status conservatism unchanged (source_metadata passes through untrimmed)

🤖 Generated with Claude Code

Round-2 phase 4. The final SSE event (and the non-stream answer JSON) shipped
every source's full chunk content plus server-only context, landing as one
large frame after the prose had already streamed — pure transfer/serialize
tail on answer completion.

toClientAnswerPayload (new answer-client-payload.ts) now trims sources at the
ROUTE BOUNDARY only:

- Drops adjacent_context, memory_cards, table_facts, index_unit, and
  document_summary — audited to have zero client consumers (generation
  packing and evidence building consume them server-side, before the trim).
- Truncates content to 700 chars at a word boundary; the client reads content
  only as the snippet fallback (retrieval_synopsis ?? content,
  answer-render-policy.ts). retrieval_synopsis, source_metadata,
  document_labels, scoring, and identity fields pass through untouched.
- rag.ts, both cache layers, eval behavior, and logAnswerDiagnostics see the
  full answer unchanged; sourceGovernanceWarnings still compute from the full
  sources before the trim (ordering documented at both call sites).
- Trim is non-mutating, so cached answers keep full sources (unit-tested).

A representative 8-source answer payload shrinks by more than half
(unit-tested lower bound; real chunks are typically larger than the fixture).

Verified: tsc, eslint (0 warnings), prettier, targeted vitest including new
answer-client-payload suite (125 tests green), and full verify:ui —
124 passed / 0 failed with phases 0-4 stacked.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@supabase

supabase Bot commented Jul 11, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project sjrfecxgysukkwxsowpy because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 34a0a914-405c-4210-882e-e7e94398652b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/perf-r2-payload-trim
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch claude/perf-r2-payload-trim

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0c68d07d9e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +27 to +30
const trimmed: SearchResult = {
...source,
content: truncateAtWordBoundary(source.content ?? "", clientSourceContentMaxChars),
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep full source text for client safety extraction

When an answer source is longer than 700 chars, this replaces source.content before it reaches the browser. The dashboard still computes safety findings from answer.sources[].content (src/lib/clinical-safety.ts:117-119, called from ClinicalDashboard.tsx:2771), not just snippets; if the only contraindication/dose-limit/monitoring phrase is past the cutoff and no quote card duplicates it, the Safety findings panel drops that warning. A focused test that runs toClientAnswerPayload before extractSafetyFindings with a phrase after char 700 would reproduce it.

Useful? React with 👍 / 👎.

@BigSimmo
BigSimmo merged commit a21a2a9 into claude/perf-r2-auth-roundtrip Jul 11, 2026
4 checks passed
BigSimmo added a commit that referenced this pull request Jul 11, 2026
… phase 3 — auth) (#481)

* perf: skip the proxy session-refresh getUser for API routes

Round-2 phase 3, auth part (isolated for security review). Every
authenticated request previously paid two serial auth-server round trips:
src/proxy.ts ran supabase.auth.getUser() for every matched path including
/api/*, and the route handler then independently validated the caller via
getOptionalAuthenticatedUser. Nothing consumed the proxy's result on API
routes — the call existed only to keep session cookies fresh.

The proxy now skips only the session-refresh getUser for /api/* paths:

- API handlers keep validating the caller themselves (bearer token and/or
  @supabase/ssr cookie) — no authorization decision moves or weakens, and the
  handlers remain fail-closed exactly as before.
- The CSP nonce header still applies to every matched response including
  /api/* (matcher unchanged).
- Session cookies still refresh on every page navigation, and the browser
  Supabase client refreshes its own token (autoRefreshToken) for bearer-based
  API calls, so long-lived sessions behave as before.

Deliberately NOT done: the trusted-internal-header design (proxy forwards a
validated user id for handlers to trust) was rejected — handlers validating
tokens independently is the stronger defense-in-depth posture. The legacy
cookie-token extraction in supabase/auth.ts was left untouched: tests define
legacy sb-access-token / plain-JSON cookie support as intended behavior.

New tests pin the contract: getUser is skipped for /api/* (CSP still
stamped), runs on page navigations with an sb- cookie, and never runs without
one. Existing CSP nonce tests unchanged and green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* perf: trim the answer payload to what the client renders (#482)

* fix: preserve cookie refresh and safety evidence

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant